"""
@description: HTTP related utils.
@author: Zzay
@create: 2022/07/14
"""
import time
import json
import queue
import requests

# constant
MESSAGE_QUEUE = queue.Queue()

def http_post(url, event_type, event_location, event_desc, event_image, elderly_person_id=-1):
    """
    Interact with SpringBoot backend using HTTP POST.
    :param url: URL of the interface on SpringBoot backend
    :param event_type: Type of the event
    :param event_location: Location where the event occur
    :param event_desc: Description of the event
    :param elderly_person_id: The corresponding elderly person's ID
    :return: Response containing data
    """
    headers = {
        "Content-Type": "application/json; charset=UTF-8"
    }
    current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    data_json = {
        # "id": 0,  # id=0 means insert; id=1 means update;
        "eventDesc": event_desc,
        "eventType": event_type,
        "eventDate": current_time,
        "eventLocation": event_location,
        "eventImage": event_image,
        "elderlyPersonId": elderly_person_id
    }
    MESSAGE_QUEUE.put(data_json)
    response = requests.post(url=url,
                             data=json.dumps(data_json),
                             headers=headers).text
    return response
